<?php
namespace app\admin\controller;

use app\admin\service\HomeService;
use app\admin\service\UserService;
use app\common\model\commonModel;
use app\common\model\trResource;
use app\common\util\ErrorCode;
use app\common\util\Tools;
use think\facade\Cache;

class AttachController extends CommonController
{
    public function handOut($_type){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        switch ($_type){
            case "saveWxPath": // 添加小程序的页面路径地址
                $method = "addWxPath";
                break;
            case "getWxPath": // 添加小程序的页面路径地址
                $method = "getWxPath";
                break;
            case "getUserDefaultAvatar":
                $method = "getUserDefaultAvatar";
                break;
            case "getProvince": // 获取省市区数据
                $method = "getProvince";
                break;
            case "clearCache": // 清除所有缓存
                $method = "getClearCache";
                break;
            case "userList": // 获取所有用户列表
                $method = "getUserList";
                break;
            default:
                return $this->returnData = ErrorCode::$errorCode["fail"];
                break;
        }
        return $this->{$method}();
    }

    public function getClearCache(){
        Cache::clear();
        return $this->return_result($this->returnData);
    }

    // 获取省市级
    public function getProvince(){
        $this->returnData["data"] = Tools::getPCAData();
        return $this->return_result($this->returnData);
    }

    // 添加小程序的页面路径地址
    public function addWxPath(){
        if(empty($this->params["title"])){
            return $this->return_result(ErrorCode::$errorCode['params_not_exist'], "title");
        }
        if(empty($this->params["path"])){
            return $this->return_result(ErrorCode::$errorCode['params_not_exist'], "path");
        }
        $result = commonModel::toAdd(wxPathTable, [
            "title" => $this->params["title"],
            "url" => $this->params["path"],
        ]);
        if(isset($result["code"])){
            return $result;
        }
        return $this->return_result($this->returnData);
    }

    // 获取小程序的页面路径地址
    public function getWxPath(){
        $this->returnData["data"] = commonModel::getFields(wxPathTable, "title,url", ["show" => 1], false);
        return $this->return_result($this->returnData);
    }

    public function getUserDefaultAvatar(){
        $this->returnData["data"] = [
            "avatarList" => [
                ["id" => md5("/attachment/default/avatar_m.png"), "label" => "默认头像A", "url" => "/attachment/default/avatar_m.png"],
                ["id" => md5("/attachment/default/avatar_w.png"), "label" => "默认头像B", "url" => "/attachment/default/avatar_w.png"],
            ]
        ];
        foreach ($this->returnData["data"]["avatarList"] as $k => $item){
            if(!trResource::getValue("url", ["id" => $item["id"]])) {
                $info = pathinfo($item["url"]);
                trResource::add([
                    'id' => $item["id"],
                    'name' => $item['id'] . "." . $info["extension"],
                    'location' => explode('/', request()->path())[0],
                    'url' => $item['url'],
                    "user_id" => $this->getSessionIdVar($this->params['token'], 'id'),
                    "is_delete" => 2,
                    'time' => time()
                ]);
            }
            $this->returnData["data"]["avatarList"][$k]["url"] = tome_dia($item['url']);
        }
        return $this->return_result($this->returnData);
    }

    // 调用小程序云开发接口
    public function getWxAppDevelop(){
        if ($this->returnData["code"] !== 0) {
            return $this->return_result($this->returnData);
        }
        $service = $this->app->invokeClass(HomeService::class, [$this->returnData, $this->params]);
        $this->returnData = $service->getWxAppCloudDevelop();
        return $this->return_result($this->returnData);
    }

    //获取所有用户列表
    public function getUserList(){
        $service = new UserService($this->returnData, $this->params);
        $this->returnData = $service->getAll();
        return $this->return_result($this->returnData);
    }
}
